package com.kk.common;


import lombok.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author: kevin
 * @data: 2018-08-17 09:54
 * @desc: 公共返回结果体
 * 关于static： 1.static方法中不能使用this,故通过调用构造方法来实现
 * 2.static方法中只能调用静态方法和静态变量，不能调用非静态的方法和变量，非静态方法可以调用静态方法和静态变量
 */
@Data
public class Result<T> {

    private static Logger logger = LoggerFactory.getLogger(Result.class);

    //返回响应状态码
    private int status;
    //返回响应数据
    private T data;
    //返回响应信息
    private String msg;

    public Result() {
    }

    public Result(T data) {
        this.status = StatusConst.SUCCESS;
        this.data = data;
        this.msg = "success";
    }

    public Result(int status, String msg) {
        this.status = status;
        this.msg = msg;
    }

    public Result(int status, T data, String msg) {
        this.status = status;
        this.data = data;
        this.msg = msg;
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(data);
    }

    public static <T> Result<T> error(Object status, String msg) {
        if (status instanceof String) {
            return new Result<>(Integer.parseInt((String) status), msg);
        } else if (status instanceof Integer) {
            return new Result<>((Integer) status, msg);
        } else {
            return new Result<>(Integer.parseInt(status.toString()), msg);
        }
    }

}
